package org.tang.utils;

import cn.hutool.http.HttpUtil;
import com.google.gson.Gson;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.tang.entity.Clients;
import org.tang.entity.ClientsResult;

import java.util.*;
import java.util.stream.Collectors;

public class SRSUtils {

    public static List<String> BLACK_LIST=new ArrayList<>();

    public static Gson gson = new Gson();
    public static final String URL = "http://192.168.6.88:1985/";

    //获取srs的clients
    public static Map<String, List<String>> getClients() {
        String clientPath = "api/v1/clients/";
        String result = HttpUtil.get(URL + clientPath);
        ClientsResult clientsResult = gson.fromJson(result, ClientsResult.class);
        List<Clients> clients = clientsResult.getClients();
        //获取所有clientId，第一个是自带的，需要把他剔除掉
        for (Clients client : clients) {
            System.out.println(client.getId());
        }
        clients.remove(0);

        //对客户端用户进行分组
        Map<String, List<Clients>> groupResult = clients.stream().collect(Collectors.groupingBy(Clients::getType));
        //得到推送方的客户
        List<Clients> publishClient = Optional.ofNullable(groupResult.get("fmle-publish")).orElse(Collections.EMPTY_LIST);
        //得到接受端的客户
        List<Clients> playClient = Optional.ofNullable(groupResult.get("rtmp-play")).orElse(Collections.EMPTY_LIST);
        List<String> publishClientIds = publishClient.stream().map(client->client.getId()+"-"+client.getIp()).collect(Collectors.toList());
        List<String> playClientIds = playClient.stream().map(client->client.getId()+"-"+client.getIp()).collect(Collectors.toList());
        System.out.println("发送方clientId");
        publishClientIds.forEach(publishId -> System.out.println(publishId));
        System.out.println("播放方clientId");
        playClientIds.forEach(clientId -> System.out.println(clientId));
        Map<String, List<String>> map = new HashMap<>();
        map.put("publish", publishClientIds);
        map.put("play", playClientIds);
        return map;
    }

    //剔除调客户端 client==>clientId - ip
    public static void kickoffClients(String client){
        String[] clientArray = client.split("-");
        String address=URL+"api/v1/clients/"+clientArray[0];
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpDelete httpDelete=new HttpDelete(address);
        CloseableHttpResponse response=null;
        try{
            response=httpClient.execute(httpDelete);
        }catch (Exception e){
            System.out.println("请求执行异常");
        }
        BLACK_LIST.add(clientArray[1]);
    }

    //查询所有的被限制的推流以及拉流的客户端
    public static List<String> queryBannedClients(){
        return BLACK_LIST;
    }

    //恢复客户端的推流/拉流
    public static String recoveryClient(String client){
        String[] clientArray = client.split("-");
        String bannedIp = clientArray[1];
        if(!BLACK_LIST.contains(bannedIp)){
            return "该客户端未被限制，无需恢复";
        }
        BLACK_LIST.remove(bannedIp);
        return "恢复成功";
    }




    public static void main(String[] args) {
        getClients();
    }
}
